import Home from '../home/home-model.js';
import config from '../utils/config.js'
const homeModel = new Home();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    size: 10,
    news: [],
    hideHistory: false,
    hideCha: true,
    searchinput: '',
    history: [],
    title: '',
    more: true,
    moreText: '不能在拉了，到底了！'
  },
  changeBox() {
    this.setData({
      hideHistory: false,
      hideCha: false
    })
  },
  onNewsDetail(e) {
    var id = homeModel.getDataSet(e, 'index');
    wx.navigateTo({
      url: '../newsDetail/newsDetail?id=' + id,
    });
  },
  goNewsByH(e) {
    var value = homeModel.getDataSet(e, 'value');
    
    this.setData({
      hideHistory: true,
      title: value
    })
    this.getList(true, value);
  },
  getList(init, title) {
    var that = this;
    if (init) {
      this.setData({
        page: 1,
        more: true,
        title: title
      })
    }
    homeModel.getSearchData(this.data.page, this.data.size, title, (res) => {
      res.news.list.map((item, index) => {
        item.image = config.imgBaseUrl + item.image;
        item.create_time = item.create_time.replace(item.create_time.substring(item.create_time.indexOf(' ') + 1, item.create_time.length), '')
      })
      if (res.news.list.length < 1) {
        that.setData({
          more: false
        })
        return;
      }
      if (init) {
        this.setData({
          more: true,
          news: res.news.list
        })
      } else {
        this.setData({
          more: true,
          news: this.data.news.concat(res.news.list)
        })
      }
    })

  },
  search: function(e) {
    var value = e.detail.value;
    
    var that = this;
    if (!value) {
      wx.showToast({
        title: '您还没有输入关键字！',
      })
      return;
    }
 
    if (this.data.history.indexOf(value) === -1) {
      wx.setStorageSync('history', this.data.history.concat(value));
      this.setData({
        hideHistory: true,
        history: this.data.history.concat(value),
        news: []
      })
    } else {
      this.setData({
        hideHistory: true,
        news: []
      })
    }
    this.getList(true, value);
  },
  clearInput(e) {
   
    this.setData({
      hideCha: true,
      searchinput: '',
      hideHistory: false,
      news: []
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var history = wx.getStorageSync('history');
    if (history.length>0) {
       
      this.setData({
        history: this.data.history.concat(history)
      })
    }else {
     
      this.setData({
        hideHistory: true
      })
    }

  },
  clearHistory() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否删除历史记录',
      success: function(res) {
        if (res.confirm) {
          that.setData({
            history: []
          })
          wx.setStorageSync('history', []);
          that.setData({
            hideHistory: true
          })
        } else if (res.cancel) {

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  onBackHome() {
    wx.switchTab({
      url: '../home/home',
    });
  },
   

  onPullDownRefresh: function () {
    
    this.getList(true, this.data.title);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.more) {
      return false
    }
    this.setData({
      page: this.data.page + 1
    })
    
    this.getList(false, this.data.title);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})